<template>
    <div v-if="userStore.userInfo.Id">
        <h2>优惠券添加</h2>
        登录名称:{{userStore.userInfo.UserName}}
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>券类型</td>
                    <td><select v-model="a.DiscountT">
                            <option value="">请选择</option>
                            <option :value="1">代金券</option>
                            <option :value="2">折扣券</option>
                            <option :value="3">兑换券</option>
                        </select></td>
                </tr>
                <tr>
                    <td>标题</td>
                    <td><input type="text" v-model="a.Title"></td>
                </tr>
                <tr>
                    <td>副标题</td>
                    <td><input type="text" v-model="a.subTitle"></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td><input type="file" @change="imgs">
                        <img :src="a.Photo" width="200">
                    </td>
                </tr>
                <tr>
                    <td>库存</td>
                    <td><input type="text" v-model="a.Count"></td>
                </tr>
                <tr>
                    <td>每人限领</td>
                    <td><input type="text" v-model="a.PersonNumber" disabled></td>
                </tr>
                <tr>
                    <td>使用条件</td>
                    <td><input type="text" v-model="a.AmountMoney"></td>
                </tr>
                <!--是否显示--->
                <tr v-if="a.DiscountT==1">
                    <td>减免金额</td>
                    <td><input type="text" v-model="a.subMoney"></td>
                </tr>
                 <!--是否显示--->
                <tr v-if="a.DiscountT==2">
                    <td>折扣</td>
                    <td><input type="text" v-model="a.ZheKou"></td>
                </tr>
                <tr>
                    <td>有效期</td>
                    <td><input type="date" v-model="a.BeginTime"></td>
                </tr>
                <tr>
                    <td></td>
                    <td>-<input type="date" v-model="a.EndTime"></td>
                </tr>
                <tr>
                    <td>适用业务</td>
                    <td><input type="checkbox" v-model="a.UseService" value="订房" name="service">订房
                        <input type="checkbox" v-model="a.UseService" value="商城" name="service">商城
                        <input type="checkbox" v-model="a.UseService" value="堂食" name="service">堂食
                        <input type="checkbox" v-model="a.UseService" value="收款码" name="service">收款码
                    </td>
                </tr>
                <tr>
                    <td>使用须知</td>
                    <td><textarea v-model="a.UseDesc">

                    </textarea></td>
                </tr>
                <tr>
                    <td>适用门店</td>
                    <td><select v-model="a.ShopId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in data.list">{{ item.Name }}</option>
                        </select></td>
                </tr>
                <tr>
                    <td>服务电话</td>
                    <td><input type="text" v-model="a.Phone" maxlength="11"></td>
                </tr>
                <tr>
                    <td>优惠券颜色</td>
                    <td><input type="color" v-model="a.Color"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
    </div>
    <div v-else>
        请先登录
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '../../stores/user';
import axios from 'axios';
import moment from 'moment';
const router = useRouter();
const route = useRoute();
const userStore=useUserStore();

onMounted(() => {
    getShopAddress();
})

let data: any = reactive({
    list: [],
})

let a: any = reactive({
    DiscountT: '',
    Title: '',
    subTitle: '',
    Photo: '',
    Count: '',
    PersonNumber: 1,
    AmountMoney: '',
    subMoney: 0,
    ZheKou: 10,
    BeginTime: '',
    EndTime: '',
    UseService: [],
    UseDesc: '',
    ShopId: '',
    Phone: '',
    Color:''

})

const imgs: any = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        method: 'post',
        url: '/api/Imgs/UpImg',
        data: f
    })
        .then((res) => {
            console.log(res);
            a.Photo = res.data
        })

}

const add: any = () => {
    if(a.Title<2)
    {
        alert("错误");
        return;
    }
    if(a.DiscountT=="")
    {
        alert("券类型不能为空");
        return;
    }
    if(a.Title=="")
    {
        alert("标题不能为空");
        return;
    }
    if(a.subTitle=="")
    {
        alert("副标题不能为空");
        return;
    }
    if(a.Photo=="")
    {
        alert("图片不能为空");
        return;
    }
    if(a.Count=="")
    {
        alert("库存不能为空");
        return;
    }
    if(parseInt(a.Count)<0)
    {
        alert("库存只能是大于0的整数");
        return;
    }
    if(a.PersonNumber=="")
    {
        alert("每人限领不能为空");
        return;
    }
    if(a.AmountMoney=="")
    {
        alert("使用条件不能为空");
        return;
    }
    if(a.subMoney==""&&a.DiscountT==1)
    {
        alert("减免金额不能为空");
        return;
    }
    if(a.ZheKou==""&&a.DiscountT==2)
    {
        alert("折扣不能为空");
        return;
    }
    if((a.ZheKou<0||a.ZheKou>=9.9)&&a.DiscountT==2)
    {
        alert("折扣只能在0-9.9折之间");
        return;
    }
    if(a.BeginTime=="")
    {
        alert("有效期不能为空");
        return;
    }
    if(a.EndTime=="")
    {
        alert("有效期不能为空");
        return;
    }
    if(a.EndTime<a.BeginTime)
    {
        alert("时间错误");
        return;
    }
    if(a.UseService.length==0)
    {
        alert("多选框必选");
        return;
    }
    if(a.UseDesc=="")
    {
        alert("使用须知不能为空");
        return;
    }
    if(a.ShopId=="")
    {
        alert("适用门店不能为空");
        return;
    }
    if(a.Phone=="")
    {
        alert("服务电话不能为空");
        return;
    }
    if(a.Color=="")
    {
        alert("优惠券颜色不能为空");
        return;
    }
    a.UseService=a.UseService.toString();
    axios({
        url: '/api/Discount/DiscountAdd',
        method: 'post',
        data: a
    })
.then((res)=>{
    console.log(res);
    if(res.data==-1)
    {
        alert("电话号重复");
        return;
    }
    if(res.data>0)
    {
        alert("添加成功");
        router.push("/discountlist");
    }
})
}

const getShopAddress = () => {
    axios({
        url: "/api/Shop/GetShopAddressList",
        method: 'get'
    })
        .then((res) => {
            console.log(res)
            data.list = res.data
        })
}




</script>

<style scoped></style>